/*
 * 人人信用管理有限公司版权所有.
 * 
 * @Description:
 * 
 * @author: liyuliang
 * 
 * @date: 20 Apr 2017 10:44:16 am
 * 
 * 注意：本内容仅限于人人信用管理有限公司内部传阅，禁止外泄以及用于其他的商业目的.
 */

package com.rrx360.common.web.util;

import javax.el.ValueExpression;
import javax.servlet.jsp.JspFactory;
import javax.servlet.jsp.PageContext;

/**
 * Helper class used to evaluate EL expression.
 * 
 * @author liyuliang
 *
 */
public class ElHelper {

  /**
   * @param beanNameName
   * @param propertyPathName
   * @param pageContext
   * @return
   */
  public static String getPropertyOfName(String beanNameName, String propertyPathName,
      PageContext pageContext) {
    String beanName = eval(beanNameName, pageContext);
    String propertyPath = eval(propertyPathName, pageContext);
    return eval(beanName + "." + propertyPath, pageContext);
  }

  /**
   * @param exp EL expresion
   * @param pageContext
   * @return
   */
  public static String eval(String exp, PageContext pageContext) {
    ValueExpression expression = JspFactory.getDefaultFactory()
        .getJspApplicationContext(pageContext.getServletContext()).getExpressionFactory()
        .createValueExpression(pageContext.getELContext(), exp, String.class);
    return (String) expression.getValue(pageContext.getELContext());
  }

}
